package com.sxran.im.handler;

import com.sxran.im.bean.AcceptFriendApplicationHttpResp;
import com.sxran.im.controller.ImController;
import com.sxran.im.dao.ImFriendMapper;
import com.sxran.im.info.ImFriend;
import com.sxran.im.info.ImFriendInvite;
import com.sxran.im.info.ImUser;
import com.sxran.im.service.FriendInviteService;
import com.sxran.im.service.NotificationService;
import com.sxran.im.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Slf4j
@Service
public class AcceptFriendApplicationCmdHandler extends RefuseFriendApplicationCmdHandler{
    @Resource
    protected UserService userService;
    @Resource
    private ImFriendMapper imFriendMapper;

    @Override
    public Object doBeanService(ImFriendInvite json) {
        // 修改申请,通知的状态。
        this.updateState(json,100);
        //
        AcceptFriendApplicationHttpResp acceptFriendApplicationHttpResp = new AcceptFriendApplicationHttpResp();
        acceptFriendApplicationHttpResp.setSysMeg("我们已经是好友啦");
        acceptFriendApplicationHttpResp.setHelloMeg("你好,很高兴遇见你");
        // 设置最新的用户信息。
        String fromUid = json.getFromUid();
        String curUserId = ImController.getCurUserId();
//        System.out.println("添加内容:"+ curUserId + "|" +fromUid );
        ImUser imUser = userService.selectByUserId(fromUid);
        acceptFriendApplicationHttpResp.setApplicationUserInfo(imUser);
        // 自己表添加一条记录
        ImFriend friend = new ImFriend();
        friend.setUserId(curUserId);
        friend.setFriendUid(fromUid);
        friend.setCreatTime(new Date());
        imFriendMapper.insert(friend);
        // 对方也添加一条记录。
        friend.setFriendUid(curUserId);
        friend.setUserId(fromUid);
        imFriendMapper.insert(friend);
        return acceptFriendApplicationHttpResp;
    }

}
